﻿#include "maco.h"
extern QMap<QString,TcpClient*> clientList;
QString recvvalue;
intListener recfl;

/**
 * @brief 构造函数
 */
maco::maco()
{
    recfl.set(0);
    connect(&recfl, &intListener::valueChanged, this,&maco::readyRead_Slot);

}

/**
 * @brief 析构函数
 */
maco::~maco()
{

}

Q_INVOKABLE int maco::sbChange(QString index)
{
    hfClient = clientList[index];
    return 0;
}

// 接收消息槽函数
void maco::readyRead_Slot()
{
    if(recfl.get()==0){

    }else{
        QString Data=recvvalue.remove(QRegExp("\\s"));
        recfl.set(0);
        QString ty = Data.mid(4,4);
        QString wendubs = Data.mid(8,4);
        QString isright = Data.mid(12,4);
        QString isright2 = Data.mid(16,4);
        if(ty=="003C"){
            if(isright=="0002"&&isright2=="0005"){
                QString addatav;
                if(litfl==1){
                    addatav=Data.mid(20,4);
                }else if(litfl==2){
                    addatav=Data.mid(24,4);
                }else if(litfl==3){
                    addatav=Data.mid(28,4);
                }else if(litfl==4){
                    addatav=Data.mid(32,4);
                }
                bool ok;
                int adval = addatav.toInt(&ok,16);
                emit advalread(QString::number(adval));
            }else if(isright=="0002"&&isright2=="0009"){
                QString dj = Data.mid(24,4);
                bool ok;
                int dj1 = dj.toInt(&ok,16);    //以16进制读入
                QString dj11 = QString::number(dj1);
                emit spbsread(dj11);
            }else if(isright=="0002"&&isright2=="000F"){
                QString dj = Data.mid(28,4);
                qDebug()<<dj;
                bool ok;
                int dj1 = dj.toInt(&ok,16);    //以16进制读入
                QString dj11 = QString::number(dj1);
                emit xzbsread(dj11);
            }else if(isright=="0002"&&isright2=="0013"){
                QString yg = Data.mid(24,4);
                bool ok;
                int yingguang = yg.toInt(&ok,16);    //以16进制读入
                double dyg = yingguang;
                QString yg1 = QString::number(dyg/10);
                emit gqread(yg1);
            }else if(isright=="0004"&&isright2=="0001"){
                QStringList csv;
                QString yg = Data.mid(20,4);
                bool ok;
                int yingguang = yg.toInt(&ok,16);    //以16进制读入
                double dyg = yingguang;
                QString yg1 = QString::number(dyg/100);
                csv.append(yg1);
                yg = Data.mid(24,4);
                ok;
                yingguang = yg.toInt(&ok,16);    //以16进制读入
                dyg = yingguang;
                yg1 = QString::number(dyg/100);
                csv.append(yg1);
                yg = Data.mid(28,4);
                ok;
                yingguang = yg.toInt(&ok,16);    //以16进制读入
                dyg = yingguang;
                yg1 = QString::number(dyg/100);
                csv.append(yg1);
                yg = Data.mid(32,4);
                ok;
                yingguang = yg.toInt(&ok,16);    //以16进制读入
                dyg = yingguang;
                yg1 = QString::number(dyg/100);
                csv.append(yg1);
                yg = Data.mid(36,4);
                ok;
                yingguang = yg.toInt(&ok,16);    //以16进制读入
                dyg = yingguang;
                yg1 = QString::number(dyg/100);
                csv.append(yg1);
                yg = Data.mid(40,4);
                ok;
                yingguang = yg.toInt(&ok,16);    //以16进制读入
                dyg = yingguang;
                yg1 = QString::number(dyg/100);
                csv.append(yg1);
                yg = Data.mid(44,4);
                ok;
                yingguang = yg.toInt(&ok,16);    //以16进制读入
                dyg = yingguang;
                yg1 = QString::number(dyg/100);
                csv.append(yg1);
                yg = Data.mid(48,4);
                ok;
                yingguang = yg.toInt(&ok,16);    //以16进制读入
                dyg = yingguang;
                yg1 = QString::number(dyg/100);
                csv.append(yg1);
                emit gqcsread(csv);
            }

        }else if(ty=="003D"){
            if(isright=="0001"&&isright2=="0002"){
                QString wen = Data.mid(20,4);
                bool ok;
                int wendu = wen.toInt(&ok,16);    //以16进制读入
                double wd = wendu;
                QString wd1 = QString::number(wd/100);
                emit rgwd(wd1);
            }else if(isright=="0004"&&isright2=="0001"){
                int kbfl=20;
                QVariantList sjwcs;
                QStringList val;
                for(int i = 0; i < 12 ; i++){
                    for(int j = 1;j < 9; j++){
                        QString kdata = Data.mid(kbfl,4);
                        int kdatai;
                        bool ok;
                        kdatai = kdata.toInt(&ok,16);    //以16进制读入
                        double kdatad = kdatai;
                        QString kdatas = QString::number(kdatad/100, 'f', 2);
                        kbfl+=4;
                        val.append(kdatas);
                    }
                    sjwcs.append(val);
                    val.clear();
                }
                emit sjwcsread(sjwcs);
            }else if(isright=="0004"&&isright2=="000A"){
                int kbfl=20;
                QVariantList sjwcs;
                QStringList val;
                for(int i = 0; i < 2 ; i++){
                    for(int j = 0;j < 6; j++){
                        QString kdata = Data.mid(kbfl,4);
                        int kdatai;
                        bool ok;
                        kdatai = kdata.toInt(&ok,16);    //以16进制读入
                        double kdatad = kdatai;
                        QString kdatas = QString::number(kdatad/100, 'f', 2);
                        kbfl+=8;
                        val.append(kdatas);
                    }
                    sjwcs.append(val);
                    val.clear();
                    kbfl=24;
                }
                emit kbcsread(sjwcs);
            }else if(isright=="000F"&&isright2=="0001"){
                QString wen = Data.mid(20,4);
                bool ok;
                int wendu = wen.toInt(&ok,16);    //以16进制读入
                double wd = wendu;
                QString wd1 = QString::number(wd/100);
                emit shwdd(wd1);
            }
        }else if(ty=="003E"){
            if(isright=="000A"&&isright2=="0001"){
                int kbfl=20;
                QVariantList bjval;
                QStringList val;
                for(int i = 0; i < 8 ; i++){
                    for(int j = 1;j < 49; j++){
                        QString kdata = Data.mid(kbfl,4);
                        int kdatai;
                        bool ok;
                        kdatai = kdata.toInt(&ok,16);    //以16进制读入
                        QString kdatas = QString::number(kdatai);
                        val.append(kdatas);
                        kbfl+=4;
                    }
                    bjval.append(val);
                    val.clear();
                }
                emit bjpcread(bjval);
            }
        }
    }
}

// 热盖温度读取
void maco::rgwdr()
{
    QString type = "0001";
    QString type2 = "0002";
    QString wen2 = "0000";
    QString end2 = "0000";
    int len = 10;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+wen2+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 热盖温度读取停止
void maco::rgwdrs()
{
    QString type = "0001";
    QString type2 = "0003";
    QString wen2 = "0000";
    QString end2 = "0000";
    int len = 10;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+wen2+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 热盖温度设置
void maco::rgwds(double temp)
{
    QString type = "0001";
    QString type2 = "0003";
    double wen = temp*100;
    int wen1 = wen;
    QString wen2 = QString("%1").arg(wen1, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 10;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+wen2+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 荧光灯开关
void maco::dkg(QString flag)
{
    if(flag=="FAM 通道开"){
        litopfl=1;
        QString type = "0002";
        QString type2 = "0001";
        QString type3 = "0001";
        QString end2 = "0000";
        int len = 10;
        QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
        QString data = header+frame_Type3+len1+type+type2+type3+end2+end_frame;
        QString finalData;
        for (int i = 0; i < data.length(); ++i) {
            finalData.append(data.at(i));
            // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
            if ((i+1)%2==0) {
                finalData.append(' ');
            }
        }
        QByteArray finalInfo=common->GetSendData(finalData);
        hfClient->ClientSendingData(finalInfo);
    }else if(flag=="VIC 通道开"){
        litopfl=2;
        QString type = "0002";
        QString type2 = "0001";
        QString type3 = "0002";
        QString end2 = "0000";
        int len = 10;
        QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
        QString data = header+frame_Type3+len1+type+type2+type3+end2+end_frame;
        QString finalData;
        for (int i = 0; i < data.length(); ++i) {
            finalData.append(data.at(i));
            // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
            if ((i+1)%2==0) {
                finalData.append(' ');
            }
        }
        QByteArray finalInfo=common->GetSendData(finalData);
        hfClient->ClientSendingData(finalInfo);
    }else if(flag=="ROX 通道开"){
        litopfl=3;
        QString type = "0002";
        QString type2 = "0001";
        QString type3 = "0003";
        QString end2 = "0000";
        int len = 10;
        QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
        QString data = header+frame_Type3+len1+type+type2+type3+end2+end_frame;
        QString finalData;
        for (int i = 0; i < data.length(); ++i) {
            finalData.append(data.at(i));
            // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
            if ((i+1)%2==0) {
                finalData.append(' ');
            }
        }
        QByteArray finalInfo=common->GetSendData(finalData);
        hfClient->ClientSendingData(finalInfo);
    }else if(flag=="CY5 通道开"){
        litopfl=4;
        QString type = "0002";
        QString type2 = "0001";
        QString type3 = "0004";
        QString end2 = "0000";
        int len = 10;
        QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
        QString data = header+frame_Type3+len1+type+type2+type3+end2+end_frame;
        QString finalData;
        for (int i = 0; i < data.length(); ++i) {
            finalData.append(data.at(i));
            // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
            if ((i+1)%2==0) {
                finalData.append(' ');
            }
        }
        QByteArray finalInfo=common->GetSendData(finalData);
        hfClient->ClientSendingData(finalInfo);
    }
}
// 荧光灯全开
void maco::dqk()
{
    litopfl=5;
    QString type = "0002";
    QString type2 = "0003";
    QString type3 = "0005";
    QString end2 = "0000";
    int len = 10;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 荧光灯全关
void maco::dqg()
{
    litopfl=0;
    QString type = "0002";
    QString type2 = "0004";
    QString type3 = "0005";
    QString end2 = "0000";
    int len = 10;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// AD单次采集
void maco::ad1time()
{
    QString type = "0002";
    QString type2 = "0005";
    QString type3 = "0000";
    QString end2 = "0000";
    int len = 10;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// AD连续采集
void maco::adtimes(int row,int column,int times,int cyc,QString td,int flag)
{
    currentRow = row;
    currentColumn = column;
    QString type = "0002";
    QString type2 = "000D";
    int kong = currentRow+1+(currentColumn-1)*8;
    QString type3 = QString("%1").arg(kong, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(litopfl, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i){
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
    // 延迟0.2秒左右
    for(int i=0; i < 1000000; i++)
    {
        //QCoreApplication::processEvents(QEventLoop::AllEvents);    //去处理本线程的事件循环，避免本线程被堵塞
        QCoreApplication::processEvents(QEventLoop::AllEvents, 5);//如果不够频繁，可以增加第二参数来缓解卡顿
    }
    int adtimes=times;
    if(flag==0){
        int z = cyc;
        int cyc5;
        if(z==0){
            cyc5 = 200;
        }else{
            cyc5 = z;
        }
        timSend5->setInterval(cyc5);
        connect(timSend5,&QTimer::timeout,this,[=]()
        {
            ad1time();
            adfl++;
            if(adfl>=adtimes){
                disconnect(timSend5,0,0,0);
                timSend5->stop();
                adfl=0;
            }
        });
        timSend5->start();
    }else if(flag==1){
        disconnect(timSend5,0,0,0);
        timSend5->stop();
        adfl=0;
    }
}
// 横向电机读取
void maco::spr(int row, int column)
{
    currentColumn = column;
    djhlie = currentColumn;
    QString type = "0002";
    QString type2 = "0009";
    QString type3 = QString("%1").arg(column, 4, 16, QLatin1Char('0'));
    QString type4 = "0000";
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 横向电机写入
void maco::spw(int row, int column, int value)
{
    currentRow = row;
    currentColumn = column;
    djhlie = currentColumn;
    QString type = "0002";
    QString type2 = "000A";
    QString type3 = QString("%1").arg(column, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(value, 4, 16, QLatin1Char('0'));
    QString slie = "param_"+QString::number(djhlie);
    QString sql="UPDATE Settings SET " + slie + "=:newValue WHERE ID=25";
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    query.prepare(sql);
    query.bindValue(":newValue", QString::number(value)); // 绑定新的值
    if (!query.exec()) {
        qDebug() << "Error:" << query.lastError().text();
    }
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 横向电机运动
void maco::spy(int row, int column)
{
    currentRow = row;
    currentColumn = column;
    djhlie = currentColumn;
    QString type = "0002";
    QString type2 = "0007";
    QString type3 = QString("%1").arg(column, 4, 16, QLatin1Char('0'));
    QString type4 = "0000";
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 横向电机复位
void maco::spf()
{
    QString type = "0002";
    QString type2 = "0008";
    QString type3 = "0000";
    QString type4 = "0000";
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 横向电机润脂测试
void maco::sprte(QString speedType,QString times)
{
    QString type = "0002";
    QString type2 = "000B";
    QString type3 = "0000";
    if(speedType=="高"){
        type3="0001";
    }else if(speedType=="中"){
        type3="0002";
    }else if(speedType=="低"){
        type3="0003";
    }
    QString type4 = QString("%1").arg(times.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 横向电机润脂停止
void maco::sprst()
{
    QString type = "0002";
    QString type2 = "000C";
    QString type3 = "0000";
    QString type4 = "0000";
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 旋转电机读取
void maco::xzr(int row,int column)
{
    qDebug()<<row<<column;
    currentRow = row;
    currentColumn = column;
    QString type="0002";
    QString type2="000F";
    int kong = currentRow+1+(currentColumn)*8;
    QString type3 = QString("%1").arg(kong, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(litopfl, 4, 16, QLatin1Char('0'));
    QString zw = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 14;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+zw+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 旋转电机写入
void maco::xzw(int row, int column, QString value)
{
    currentRow = row;
    currentColumn = column;
    QString type="0002";
    QString type2="0010";
    int kong = currentRow+1+(currentColumn)*8;
    QString type3 = QString("%1").arg(kong, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(litopfl, 4, 16, QLatin1Char('0'));
    QString zw = QString("%1").arg(value.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 14;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+zw+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 旋转电机运动
void maco::xzy(int row, int column)
{
    currentRow = row;
    currentColumn = column;
    QString type="0002";
    QString type2="000D";
    int kong = currentRow+1+(currentColumn-1)*8;
    QString type3 = QString("%1").arg(kong, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(litopfl, 4, 16, QLatin1Char('0'));
    QString zw = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 14;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+zw+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 旋转电机复位
void maco::xzf()
{
    QString type="0002";
    QString type2="000E";
    QString type3 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString zw = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 14;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+zw+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 旋转电机润脂测试
void maco::xzrte(QString speedType, QString times)
{
    QString type = "0002";
    QString type2 = "0011";
    QString type3 = "0000";
    if(speedType=="高"){
        type3="0001";
    }else if(speedType=="中"){
        type3="0002";
    }else if(speedType=="低"){
        type3="0003";
    }
    QString type4 = QString("%1").arg(times.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 旋转电机润脂停止
void maco::xzrst()
{
    QString type = "0002";
    QString type2 = "0012";
    QString type3 = "0000";
    QString type4 = "0000";
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 光强通道选择
void maco::gqt(QString channel)
{
    if(channel=="FAM"){
        litqrfl=1;
    }else if(channel=="VIC"){
        litqrfl=2;
    }else if(channel=="ROX"){
        litqrfl=3;
    }else if(channel=="CY5"){
        litqrfl=4;
    }
}
// 光强读取
void maco::gqr()
{
    QString type = "0002";
    QString type2 = "0013";
    QString type3 = QString("%1").arg(litqrfl, 4, 16, QLatin1Char('0'));
    QString type4 = "0000";
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 光强写入
void maco::gqw(QString value)
{
    QString type = "0002";
    QString type2 = "0014";
    QString type3 = QString("%1").arg(litqrfl, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(value.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 光强系数读取
void maco::gqxr()
{
    QString type = "0004";
    QString type2 = "0001";
    QString ty;
    ty.append(type);
    ty.append(type2);
    QString end2 = "0000";
    int len = 8;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+ty+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i){
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 光强系数写入
void maco::gqxw(QStringList xs)
{
    QString type = "0004";
    QString type2 = "0002";
    QString ty;
    ty.append(type);
    ty.append(type2);
    int xss1=xs[0].toDouble()*100;
    int xss2=xs[1].toDouble()*100;
    int xss3=xs[2].toDouble()*100;
    int xss4=xs[3].toDouble()*100;
    int xss5=xs[4].toDouble()*100;
    int xss6=xs[5].toDouble()*100;
    int xss7=xs[6].toDouble()*100;
    int xss8=xs[7].toDouble()*100;
    QString xs1 = QString("%1").arg(xss1, 4, 16, QLatin1Char('0'));
    QString xs2 = QString("%1").arg(xss2, 4, 16, QLatin1Char('0'));
    QString xs3 = QString("%1").arg(xss3, 4, 16, QLatin1Char('0'));
    QString xs4 = QString("%1").arg(xss4, 4, 16, QLatin1Char('0'));
    QString xs5 = QString("%1").arg(xss5, 4, 16, QLatin1Char('0'));
    QString xs6 = QString("%1").arg(xss6, 4, 16, QLatin1Char('0'));
    QString xs7 = QString("%1").arg(xss7, 4, 16, QLatin1Char('0'));
    QString xs8 = QString("%1").arg(xss8, 4, 16, QLatin1Char('0'));
    ty.append(xs1);
    ty.append(xs2);
    ty.append(xs3);
    ty.append(xs4);
    ty.append(xs5);
    ty.append(xs6);
    ty.append(xs7);
    ty.append(xs8);
    QString end2 = "0000";
    int len = 24;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type3+len1+ty+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i){
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 背景偏差写入
void maco::bjbc(QVariantList xs)
{
    QVector<QStringList> result;
    for (const QVariant &variant : xs) {
        if (variant.canConvert<QStringList>()) {
            QStringList stringList = variant.value<QStringList>();
            result.append(stringList);
        }
    }
    QString type = "000A";
    QString type2 = "0001";
    QString ty;
    ty.append(type);
    ty.append(type2);
    for(int i=0;i<result.length();i++){
        for(int j=0;j<result[i].length();j++){
            ty.append(QString("%1").arg(result[i][j].toInt(), 4, 16, QLatin1Char('0')));
        }
    }
    QString end2 = "0000";
    int len = 776;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type5+len1+ty+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 背景偏差读取
void maco::bjdq()
{
    QString type = "000A";
    QString type2 = "0002";
    QString ty;
    ty.append(type);
    ty.append(type2);
    QString end2 = "0000";
    int len = 8;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type5+len1+ty+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i){
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 背景偏差导入
QStringList maco::bjdr()
{
    QStringList value;

#ifdef Q_OS_WINDOWS

    QString desktopDir=QStandardPaths::writableLocation(QStandardPaths::DesktopLocation);
    QString path=QFileDialog::getOpenFileName(NULL,"选择Excel",desktopDir,"*.xlsx *.xls");
    if(path==NULL){
        return {};
    }
    QAxObject *excel = NULL;
    QAxObject *workbooks = NULL;
    QAxObject *workbook = NULL;
    excel = new QAxObject(this);
    if (!excel->setControl("Excel.Application"))
    {
        excel->setControl("ket.Application");
    }
    if (!excel)
    {
        QMessageBox::critical(NULL, "错误信息", "EXCEL对象丢失");
        return {};
    }
    excel->dynamicCall("SetVisible(bool)", false);
    workbooks = excel->querySubObject("WorkBooks");
    workbook = workbooks->querySubObject("Open(const QString&)",path);
    QAxObject * worksheet = workbook->querySubObject("WorkSheets(int)", 1); // 获取第一个工作sheet
    QAxObject * usedrange = worksheet->querySubObject("UsedRange");//获取该sheet的使用范围对象
    QAxObject * rows = usedrange->querySubObject("Rows");
    QAxObject * columns = usedrange->querySubObject("Columns");

    /*获取行数和列数*/
    int intCols = columns->property("Count").toInt();
    int intRows = rows->property("Count").toInt();

    /*获取excel内容*/
    for (int i = 1; i <  intRows+1; i++)  //行
    {
        for (int j = 2; j <  intCols+1; j++)
        {
            QAxObject *cell = worksheet->querySubObject("Cells(int,int)", i, j);
            value.append(QString::number(cell->dynamicCall("Value2()").toDouble(), 'f', 2));
        }
    }
    // 关闭excel
    workbook->dynamicCall("Close(Boolean)",true);
    excel->dynamicCall("Quit(void)");
    delete excel;
    excel = NULL;
    QMessageBox::information(NULL, "表格导入提示", "表格导入成功");

#endif
    return value;
}
// 背景偏差导出
void maco::bjdc(QStringList xs)
{
#ifdef Q_OS_WINDOWS
    QString fileName = QFileDialog::getSaveFileName(nullptr, tr("保存"), "", tr("Excel 文件(*.xlsx)"));
    if(fileName != ""){
        QAxObject *excel = new QAxObject;
        if(excel->setControl("Excel.Application")){
            excel->dynamicCall("SetVisible (bool Visible)","false");//不显示窗体
            excel->setProperty("DisplayAlerts", false);//不显示任何警告信息。如果为true那么在关闭是会出现类似“文件已修改，是否保存”的提示
            QAxObject *workbooks = excel->querySubObject("WorkBooks");//获取工作簿集合
            workbooks->dynamicCall("Add");//新建一个工作簿
            QAxObject *workbook = excel->querySubObject("ActiveWorkBook");//获取当前工作簿
            QAxObject *sheets = workbook->querySubObject("Worksheets");
            if(true){
                QAxObject *worksheet = workbook->querySubObject("Worksheets(int)", 1);
                worksheet->setProperty("Name", "荧光表格");
                //调整行高
                worksheet->querySubObject("Range(const QString&)", "1:1")->setProperty("RowHeight", 30);
                //数据区
                // 写入表头
                //                int columnCount = ui->bj_tab->columnCount();
                //                for (int j = 0; j < columnCount; ++j) {
                //                    QAxObject *cell = worksheet->querySubObject("Cells(int,int)", 1, j + 1);
                //                    cell->dynamicCall("SetValue(const QVariant&)", ui->bj_tab->horizontalHeaderItem(j)->text());
                //                }

                // 写入表内容
                int fl=0;
                for (int i = 0; i < 8; ++i) {
                    for (int j = 0; j < 48; ++j) {
                        QAxObject *cell = worksheet->querySubObject("Cells(int,int)", i + 2, j + 1);
                        cell->dynamicCall("SetValue(const QVariant&)", xs[fl]);
                        fl++;
                    }
                }
            }
            workbook->dynamicCall("SaveAs(const QString&)", QDir::toNativeSeparators(fileName));//保存至fileName
            workbook->dynamicCall("Close()");//关闭工作簿
            excel->dynamicCall("Quit()");//关闭excel
            delete excel;
            excel = nullptr;
            QMessageBox::information(NULL, "表格导出提示", "表格导出成功");
        }
    }

#endif
}
// 打开仓门
void maco::cmk(int bs)
{
    QString type = "0003";
    QString type2 = "0001";
    QString type3 = QString("%1").arg(bs, 4, 16, QLatin1Char('0'));
    QString type4 = "0000";
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QString slie = "param_"+QString::number(2);
    QString sql="UPDATE Settings SET " + slie + "=:newValue WHERE ID=15";
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    query.prepare(sql);
    query.bindValue(":newValue", QString::number(bs)); // 绑定新的值
    if (!query.exec()) {
        qDebug() << "Error:" << query.lastError().text();
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 关闭仓门
void maco::cmg(int bs)
{
    QString type = "0003";
    QString type2 = "0002";
    QString type3 = QString("%1").arg(bs, 4, 16, QLatin1Char('0'));
    QString type4 = "0000";
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QString slie = "param_"+QString::number(1);
    QString sql="UPDATE Settings SET " + slie + "=:newValue WHERE ID=15";
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    query.prepare(sql);
    query.bindValue(":newValue", QString::number(bs)); // 绑定新的值
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 耗材类型选择
void maco::hclx(QString hclx)
{
    if(hclx=="八连排"){
        hctype=1;
    }else if(hclx=="单管"){
        hctype=2;
    }else if(hclx=="有裙边"){
        hctype=3;
    }else if(hclx=="无裙边"){
        hctype=4;
    }
}
// 电机上升
void maco::djss(int bs)
{
    QString type = "0003";
    QString type2 = "0003";
    QString type3 = QString("%1").arg(hctype, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(bs, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 电机复位
void maco::djfw()
{
    QString type = "0003";
    QString type2 = "0004";
    QString type3 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 加热块润脂测试
void maco::rkrzcs(QString speedType, QString times)
{
    QString type = "0003";
    QString type2 = "0005";
    QString type3 = "0000";
    if(speedType=="高"){
        type3="0001";
    }else if(speedType=="中"){
        type3="0002";
    }else if(speedType=="低"){
        type3="0003";
    }
    QString type4 = QString("%1").arg(times.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 加热块润脂停止
void maco::rktzrz()
{
    QString type = "0003";
    QString type2 = "0006";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 升降温参数读取
void maco::sjwr()
{
    QString type = "0004";
    QString type2 = "0001";
    QString type3 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 10;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 升降温参数写入
void maco::sjww(QVariantList cs)
{
    QVector<QStringList> result;
    for (const QVariant &variant : cs) {
        if (variant.canConvert<QStringList>()) {
            QStringList stringList = variant.value<QStringList>();
            result.append(stringList);
        }
    }
    QString type = "0004";
    QString type2 = "0002";
    QString ty;
    ty.append(type);
    ty.append(type2);
    for(int i=0;i<result.length();i++){
        for(int j=0;j<result[i].length();j++){
            int datava;
            datava = QString::number(result[i][j].toDouble()*100).toInt();
            ty.append(QString("%1").arg(datava, 4, 16, QLatin1Char('0')));
        }
    }
    QString end2 = "0000";
    int len = 200;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+ty+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 升温测试
void maco::sw(QString temp)
{
    QString type = "0004";
    QString type2 = "0005";
    QString type3 = "0005";
    QString type4 = QString("%1").arg(temp.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 降温测试
void maco::jw(QString temp)
{
    QString type = "0004";
    QString type2 = "0006";
    QString type3 = "0005";
    QString type4 = QString("%1").arg(temp.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 加热测试
void maco::jr(QString p)
{
    QString type = "0004";
    QString type2 = "0007";
    QString type3 = "0005";
    QString type4 = QString("%1").arg(p.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 制冷测试
void maco::zl(QString p)
{
    QString type = "0004";
    QString type2 = "0008";
    QString type3 = "0005";
    QString type4 = QString("%1").arg(p.toInt(), 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// K、B值读取
void maco::kbr()
{
    QString type = "0004";
    QString type2 = "000A";
    QString type3 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString type5 = "0000";
    QString end2 = "0000";
    int len = 14;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+type5+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// K、B值写入
void maco::kbw(QVariantList cs)
{
    QVector<QStringList> result;
    for (const QVariant &variant : cs) {
        if (variant.canConvert<QStringList>()) {
            QStringList stringList = variant.value<QStringList>();
            result.append(stringList);
        }
    }
    QString kvv;
    QMap<QString, int> keywordToValue = {
        {"A", 1},
        {"B", 2},
        {"C", 3},
        {"D", 4},
        {"E", 5},
        {"F", 6}
    };
    QString kw = "A";
    int len = 32;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString type = "0004";
    QString type2 = "000B";
    QString data = type+type2;
    int kv;
    QByteArray byteArray = kw.toUpper().toLatin1();
    char firstChar = byteArray.at(0);
    int rowIndex = firstChar - 'A'; // 将'A'到'F'转换为0到5的索引
    double v1;
    for(int i=0;i<6;i++){
        for(int j=0;j<2;j++){
            int datava;
            datava = QString::number(result[j][i].toDouble()*100).toInt();
            data.append(QString("%1").arg(datava, 4, 16, QLatin1Char('0')));
        }
    }
    QString end2 = "0000";
    QString data2 = header+frame_Type4+len1+data+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data2.length(); ++i) {
        finalData.append(data2.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 主控复位
void maco::zkfw()
{
    QString type = "0005";
    QString type2 = "0002";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 主控保存
void maco::zkbc()
{
    QString type = "0005";
    QString type2 = "0001";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 水泵开
void maco::sbo()
{
    QString type = "0006";
    QString type2 = "0001";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 水泵关
void maco::sbc()
{
    QString type = "0006";
    QString type2 = "0002";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 风扇开
void maco::fso()
{
    QString type = "0006";
    QString type2 = "0003";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 风扇关
void maco::fsc()
{
    QString type = "0006";
    QString type2 = "0004";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 恢复初始
void maco::hfcs()
{

}
// 设备自检
void maco::sbzj()
{
    QString type = "0000";
    QString type2 = "0001";
    QString type3 = "0005";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 10;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type2+len1+type+type2+type3+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
// 停止测试
void maco::sjws()
{
    QString type = "0004";
    QString type2 = "0009";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);

}
// IP修改
void maco::ckcon(QString ip,QString port){
    if(true){
        QString IP =  ip;
        int PORT = port.toInt();
        QStringList parts = IP.split('.');
        QString number1,number2,number3,number4;
        if (parts.count() == 4) {
            number1 = parts[0]; // 第一个数字
            number2 = parts[1]; // 第二个数字
            number3 = parts[2]; // 第三个数字
            number4 = parts[3]; // 第四个数字
        }
        QString num1 = QString("%1").arg(number1.toInt(), 2, 16, QLatin1Char('0'));
        QString num2 = QString("%1").arg(number2.toInt(), 2, 16, QLatin1Char('0'));
        QString num3 = QString("%1").arg(number3.toInt(), 2, 16, QLatin1Char('0'));
        QString num4 = QString("%1").arg(number4.toInt(), 2, 16, QLatin1Char('0'));
        QString Po = QString("%1").arg(PORT, 4, 16, QLatin1Char('0'));
        int len = 12;
        QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
        QString bs = "0002";
        QString bs1 = "0001";
        QByteArray byte = bs.toUtf8()+num1.toUtf8()+num2.toUtf8()+num3.toUtf8()+num4.toUtf8()+Po.toUtf8();
        QString cd1 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
        QString data = header +frame_Type4+len1+bs+bs1+num1+num2+num3+num4+Po+cd1+end_frame;
        QByteArray all;
        //分析字符串格式中是否带空格
        QString finalData;
        for (int i = 0; i < data.length(); ++i) {
            finalData.append(data.at(i));
            // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
            if ((i+1)%2==0) {
                finalData.append(' ');
            }
        }
        QByteArray finalInfo=common->GetSendData(finalData);
        hfClient->ClientSendingData(finalInfo);
    }
    // 延迟0.2秒左右
    for(int i=0; i < 100000; i++)
    {
        //QCoreApplication::processEvents(QEventLoop::AllEvents);    //去处理本线程的事件循环，避免本线程被堵塞
        QCoreApplication::processEvents(QEventLoop::AllEvents, 5);//如果不够频繁，可以增加第二参数来缓解卡顿
    }
    // 子网掩码
    if(true){
        QString IP =  "255.255.255.0";
        QStringList parts = IP.split('.');
        QString number1,number2,number3,number4;
        if (parts.count() == 4) {
            number1 = parts[0]; // 第一个数字
            number2 = parts[1]; // 第二个数字
            number3 = parts[2]; // 第三个数字
            number4 = parts[3]; // 第四个数字
        }
        QString num1 = QString("%1").arg(number1.toInt(), 2, 16, QLatin1Char('0'));
        QString num2 = QString("%1").arg(number2.toInt(), 2, 16, QLatin1Char('0'));
        QString num3 = QString("%1").arg(number3.toInt(), 2, 16, QLatin1Char('0'));
        QString num4 = QString("%1").arg(number4.toInt(), 2, 16, QLatin1Char('0'));
        int len = 10;
        QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
        QString bs = "0002";
        QString bs1 = "0002";
        QByteArray byte = bs.toUtf8()+num1.toUtf8()+num2.toUtf8()+num3.toUtf8()+num4.toUtf8();
        QString cd1 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
        QString data = header +frame_Type4+len1+bs+bs1+num1+num2+num3+num4+cd1+end_frame;
        QByteArray all;
        QString finalData;
        for (int i = 0; i < data.length(); ++i) {
            finalData.append(data.at(i));
            // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
            if ((i+1)%2==0) {
                finalData.append(' ');
            }
        }
        QByteArray finalInfo=common->GetSendData(finalData);
        hfClient->ClientSendingData(finalInfo);
    }
    // 延迟0.2秒左右
    for(int i=0; i < 100000; i++)
    {
        //QCoreApplication::processEvents(QEventLoop::AllEvents);    //去处理本线程的事件循环，避免本线程被堵塞
        QCoreApplication::processEvents(QEventLoop::AllEvents, 5);//如果不够频繁，可以增加第二参数来缓解卡顿
    }
    // 网关
    if(true){
        QString IP =  "192.168.0.1";
        QStringList parts = IP.split('.');
        QString number1,number2,number3,number4;
        if (parts.count() == 4) {
            number1 = parts[0]; // 第一个数字
            number2 = parts[1]; // 第二个数字
            number3 = parts[2]; // 第三个数字
            number4 = parts[3]; // 第四个数字
        }
        QString num1 = QString("%1").arg(number1.toInt(), 2, 16, QLatin1Char('0'));
        QString num2 = QString("%1").arg(number2.toInt(), 2, 16, QLatin1Char('0'));
        QString num3 = QString("%1").arg(number3.toInt(), 2, 16, QLatin1Char('0'));
        QString num4 = QString("%1").arg(number4.toInt(), 2, 16, QLatin1Char('0'));
        int len = 10;
        QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
        QString bs = "0002";
        QString bs1 = "0003";
        QByteArray byte = bs.toUtf8()+num1.toUtf8()+num2.toUtf8()+num3.toUtf8()+num4.toUtf8();
        QString cd1 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
        QString data = header +frame_Type4+len1+bs+bs1+num1+num2+num3+num4+cd1+end_frame;
        QByteArray all;
        //分析字符串格式中是否带空格
        for(int i = 0;i<data.size();i++)
        {
            if(data[i]==' ')
                continue;   //跳过

            int num  = data.mid(i,2).toUInt(nullptr,16);
            i++;

            all.append(num);
        }
        QString finalData;
        for (int i = 0; i < data.length(); ++i) {
            finalData.append(data.at(i));
            // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
            if ((i+1)%2==0) {
                finalData.append(' ');
            }
        }
        QByteArray finalInfo=common->GetSendData(finalData);
        hfClient->ClientSendingData(finalInfo);
    }
    // 延迟0.2秒左右
    for(int i=0; i < 100000; i++)
    {
        //QCoreApplication::processEvents(QEventLoop::AllEvents);    //去处理本线程的事件循环，避免本线程被堵塞
        QCoreApplication::processEvents(QEventLoop::AllEvents, 5);//如果不够频繁，可以增加第二参数来缓解卡顿
    }
    // MAC地址
    if(true){
        QStringList part1;
        for (int i = 0; i < 6; ++i) {
            QString part = QString("%1").arg(QRandomGenerator::global()->bounded(256), 2, 16, QChar('0')).toUpper();
            part1.append(part);
        }

        // 组合成一个完整的MAC地址字符串，并用-分隔
        QString macAddress = part1.join("-");
        QString IP =  macAddress;
        QStringList parts = IP.split('-');
        QString number1,number2,number3,number4,number5,number6;
        if (parts.count() == 6) {
            number1 = parts[0]; // 第一个数字
            number2 = parts[1]; // 第二个数字
            number3 = parts[2]; // 第三个数字
            number4 = parts[3]; // 第四个数字
            number5 = parts[4]; // 第五个数字
            number6 = parts[5]; // 第六个数字
        }
        int len = 12;
        QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
        QString bs = "0002";
        QString bs1 = "0004";
        QByteArray byte = bs.toUtf8()+number1.toUtf8()+number2.toUtf8()+number3.toUtf8()+number4.toUtf8()+number5.toUtf8()+number6.toUtf8();
        QString cd1 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
        QString data = header +frame_Type4+len1+bs+bs1+number1+number2+number3+number4+number5+number6+cd1+end_frame;
        QByteArray all;
        QString finalData;
        for (int i = 0; i < data.length(); ++i) {
            finalData.append(data.at(i));
            // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
            if ((i+1)%2==0) {
                finalData.append(' ');
            }
        }
        QByteArray finalInfo=common->GetSendData(finalData);
        hfClient->ClientSendingData(finalInfo);
    }
    QSqlDatabase db = dbHelper::openConnection();
    QSqlQuery query(db);
    QString sql= QString("Update EquipmentInfo Set Ip='%1',Port='%2' where Id='%3' ").arg(ip,port,"0");
    bool res = query.exec(sql);
    LogHelperJ::write(sql + (res?"[true]":"[false]"));
    zkbc();
}
// 荧光读取通道选择
void maco::tdxz(QString td){
    if(td=="FAM"){
        litfl=1;
    }else if(td=="VIC"){
        litfl=2;
    }else if(td=="ROX"){
        litfl=3;
    }else if(td=="CY5"){
        litfl=4;
    }
}
// 主控操作
void maco::zkcz(QString cz){
    if(cz=="主控保存"){
        zkbc();
    }else if(cz=="主控复位"){
        zkfw();
    }
}
// 水盒温度读取
void maco::shwd(){
    QString type = "000F";
    QString type2 = "0001";
    QString type3 = "0000";
    QString type4 = QString("%1").arg(0, 4, 16, QLatin1Char('0'));
    QString end2 = "0000";
    int len = 12;
    QString len1 = QString("%1").arg(len, 4, 16, QLatin1Char('0'));
    QString data = header+frame_Type4+len1+type+type2+type3+type4+end2+end_frame;
    QString finalData;
    for (int i = 0; i < data.length(); ++i) {
        finalData.append(data.at(i));
        // 如果不是字符串的最后一个字符，并且i是偶数（从0开始），则添加一个空格
        if ((i+1)%2==0) {
            finalData.append(' ');
        }
    }
    QByteArray finalInfo=common->GetSendData(finalData);
    hfClient->ClientSendingData(finalInfo);
}
